package com.jiang.myfm.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiang.myfm.R;
import com.jiang.myfm.activity.BillboardInfoActivity;
import com.jiang.myfm.adapters.BillboardItemAdapter;
import com.jiang.myfm.entities.billboard.BillboardEntity;
import com.jiang.myfm.utils.HttpService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/2.
 */
public class Findfragment_4 extends Fragment implements Callback<BillboardEntity>,BillboardItemAdapter.onChildClickListener{
    private ImageView billboardimageView;
    private RecyclerView programRecycler,anchorRecycler;
    private TextView title1TextView,title2TextView;
    private HttpService httpService;
    private BillboardEntity billboardDatas;
    private BillboardItemAdapter programAdapter;
    private BillboardItemAdapter anchorAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_4,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        billboardimageView = ((ImageView) view.findViewById(R.id.billboard_iv));
        title1TextView = (TextView) view.findViewById(R.id.title1_tv);
        title2TextView = (TextView) view.findViewById(R.id.title2_tv);
        programRecycler = (RecyclerView) view.findViewById(R.id.program_recycler);
        anchorRecycler = (RecyclerView) view.findViewById(R.id.anchor_recycler);
        LinearLayoutManager manager = new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        LinearLayoutManager manager2 = new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        programRecycler.setLayoutManager(manager);
        anchorRecycler.setLayoutManager(manager2);

        httpService = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        httpService.getText().enqueue(this);

        programAdapter = new BillboardItemAdapter(getContext());
        anchorAdapter = new BillboardItemAdapter(getContext());
        programAdapter.setListener(this);
        anchorAdapter.setListener(this);
        programRecycler.setAdapter(programAdapter);
        anchorRecycler.setAdapter(anchorAdapter);
    }

    @Override
    public void onResponse(Call<BillboardEntity> call, Response<BillboardEntity> response) {
        billboardDatas = response.body();
        Picasso.with(getContext()).load(billboardDatas.getFocusImages().getList().get(0).getPic()).into(billboardimageView);
        title1TextView.setText(billboardDatas.getDatas().get(0).getTitle());
        title2TextView.setText(billboardDatas.getDatas().get(1).getTitle());
        programAdapter.addAll(billboardDatas.getDatas().get(0).getList());
        anchorAdapter.addAll(billboardDatas.getDatas().get(1).getList());
    }

    @Override
    public void onFailure(Call<BillboardEntity> call, Throwable t) {

    }

    @Override
    public void onChildClick(int position, String title, String key, String contentType) {
        Toast.makeText(getContext(), title, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getActivity(), BillboardInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        bundle.putString("key",key);
        bundle.putString("contentType",contentType);
        intent.putExtras(bundle);
        startActivity(intent);

    }
}
